using System;
using System.Collections.Generic;
using MotionFramework.HybridCLR;
using MotionFramework.MVC;
using UnityEngine;

public class UpdaterDoneCommand : SimpleCommand
{
    public override void Execute(INotification notification)
    {
		try
		{
			LoadAssembly();

			LoadMetadataForAOTAssembly();

			LauncherApplication();
		}
		catch (Exception e)
		{
			Debug.LogError(e.ToString());
		}
    }

	/// <summary>
	/// 加载热更新动态库
	/// </summary>
	void LoadAssembly()
	{
		List<string> assemblys = new List<string>() {
			"Assembly/Assembly-CSharp.dll.bytes",
		};
		HotfixManager.Instance.LoadAssembly(assemblys);
	}

	/// <summary>
	/// 补充元数据
	/// </summary>
	void LoadMetadataForAOTAssembly()
	{
		List<string> assemblys = new List<string>() {
			"Assembly/System.dll.bytes",
			"Assembly/mscorlib.dll.bytes",
			"Assembly/System.Core.dll.bytes",
		};
		HotfixManager.Instance.LoadMetadataForAOTAssembly(assemblys);
	}

	/// <summary>
	/// 启动函数
	/// </summary>
	void LauncherApplication()
	{
		// 反射获取入口类型
		var commandName = "StartUpCommand";
		var commandType = Type.GetType(commandName + ",Assembly-CSharp");
		if (commandType == null)
		{
			Debug.LogError($"commandType {commandName} Is Null .");
			return;
		}

		// 注册入口
		GameFacade.Instance.RegisterCommand(commandName, 
			() => Activator.CreateInstance(commandType) as ICommand);

		// 启动入口
		GameFacade.Instance.SendNotification(commandName);
	}
}
